# encoding: utf-8
# @File   : file_handler.py
# @Author : 乖孩子
# @Date   : 2025/07/24 16:31:35
import json
import os


def save_authorization(token, full_response):
    """保存认证信息到文件"""
    try:
        # 创建保存目录（如果不存在）
        if not os.path.exists("auth_data"):
            os.makedirs("auth_data")

        # 保存token到文本文件
        token_path = os.path.join("auth_data", "Authorization.txt")
        with open(token_path, 'w') as file:
            file.write(token)

        # 保存完整响应到JSON文件
        json_path = os.path.join("auth_data", "Authorization.json")
        with open(json_path, 'w', encoding='utf-8') as file:
            json.dump(full_response, file, indent=4, ensure_ascii=False)

        return token_path

    except Exception as e:
        print(f"保存认证信息失败: {str(e)}")
        return None


def read_authorization():
    """读取认证信息"""
    try:
        token_path = os.path.join("auth_data", "Authorization.txt")
        if os.path.exists(token_path):
            with open(token_path, 'r') as file:
                return file.read().strip()
        return None
    except Exception as e:
        print(f"读取认证信息失败: {str(e)}")
        return None